const {
    Mysql
} = require('../tools/utils');
module.exports = async (ctx) => {
    // 获取首页列表
    let {
        from,
        uid,
        sort,
        type,
        currentPage,
        pageSize
    } = ctx.query;

    // 获取收藏列表
    if (from == 'collect' && uid) {
        let sql = `SELECT * FROM collect c JOIN video p ON p.vid=c.vid AND c.uid=${uid} LIMIT ${(currentPage-1)*pageSize},${pageSize}`;
        try {
            let list = await Mysql(sql);
            let listStr = Buffer.from(JSON.stringify(list)).toString('base64').replace(/A/g, '&').replace(/B/g, '%'); //第一次转base64
            ctx.state.data = {
                list: listStr,
                // total: total[0].total,
                code: '001'
            }
        } catch (error) {
            ctx.state.data = {
                code: '002',
                err: 'مەغلۇپ بولدى'
            }
        }
    }
    // 获取搜索结果
    if (ctx.query.from == 'search') {
        let {
            query
        } = ctx.query;
        let sql = `SELECT * from video WHERE title LIKE '%${query}%' LIMIT ${(currentPage-1)*pageSize},${pageSize}`;
        try {
            let list = await Mysql(sql);
            let listStr = Buffer.from(JSON.stringify(list)).toString('base64').replace(/A/g, '&').replace(/B/g, '%'); //第一次转base64
            ctx.state.data = {
                list: listStr,
                code: '001'
                // total: total[0].total,
            }
        } catch (error) {
            ctx.state.data = {
                code: '002',
                err: 'مەغلۇپ بولدى'
            }
        }
    }
    // 获取会员专属电影
    if (from == 'VIP') {
        let sql = `SELECT video.* from (SELECT * from video WHERE isVip=1 ORDER BY create_time DESC)video LIMIT ${(currentPage-1)*pageSize},${pageSize}`;
        try {
            let list = await Mysql(sql);
            let listStr = Buffer.from(JSON.stringify(list)).toString('base64').replace(/A/g, '&').replace(/B/g, '%'); //第一次转base64
            ctx.state.data = {
                list: listStr,
                // total: total[0].total,
                code: '001'
            }
        } catch (error) {
            ctx.state.data = {
                code: '002',
                err: 'مەغلۇپ بولدى'
            }
        }
    }
    // 根据电影类型获取列表
    if (from == 'sort') {
        if (sort == 'MONEY') {
            let sql = `SELECT video.* from (SELECT * from video WHERE video.isFree=1 OR video.price>0 ORDER BY create_time DESC)video LIMIT ${(currentPage-1)*pageSize},${pageSize}`;
            try {
                let list = await Mysql(sql);
                let listStr = Buffer.from(JSON.stringify(list)).toString('base64').replace(/A/g, '&').replace(/B/g, '%'); //第一次转base64
                ctx.state.data = {
                    list: listStr,
                    // total: total[0].total,
                    code: '001'
                }
            } catch (error) {
                ctx.state.data = {
                    code: '002',
                    err: 'مەغلۇپ بولدى'
                }
            }
            return;
        }
        let sql = `SELECT video.* from (SELECT * from video WHERE sort='${sort}' ORDER BY create_time DESC)video LIMIT ${(currentPage-1)*pageSize},${pageSize}`;
        try {
            let list = await Mysql(sql);
            let listStr = Buffer.from(JSON.stringify(list)).toString('base64').replace(/A/g, '&').replace(/B/g, '%'); //第一次转base64
            ctx.state.data = {
                list: listStr,
                // total: total[0].total,
                code: '001'
            }
        } catch (error) {
            ctx.state.data = {
                code: '002',
                err: 'مەغلۇپ بولدى'
            }
        }
    }
    if (from == 'home') {
        if (type == 'new') {
            let sql = `SELECT video.* from (SELECT * from video ORDER BY create_time DESC)video  LIMIT ${(currentPage-1)*pageSize},${pageSize}`;
            try {
                let list = await Mysql(sql);
                let listStr = Buffer.from(JSON.stringify(list)).toString('base64').replace(/A/g, '&').replace(/B/g, '%'); //第一次转base64
                ctx.state.data = {
                    list: listStr,
                    // total: total[0].total,
                    code: '001'
                }
            } catch (error) {
                ctx.state.data = {
                    code: '002',
                    err: 'مەغلۇپ بولدى'
                }
            }
        }
        if (type == 'hot') {
            let sql = `SELECT video.* from (SELECT * from video ORDER BY views DESC)video LIMIT ${(currentPage-1)*pageSize},${pageSize}`;
            try {
                let list = await Mysql(sql);
                let listStr = Buffer.from(JSON.stringify(list)).toString('base64').replace(/A/g, '&').replace(/B/g, '%'); //第一次转base64
                ctx.state.data = {
                    list: listStr,
                    // total: total[0].total,
                    code: '001'
                }
            } catch (error) {
                ctx.state.data = {
                    code: '002',
                    err: 'مەغلۇپ بولدى'
                }
            }
        }
    }
    // 获取随机推荐电影
    if (from == 'recommend') {
        let sql = `SELECT * FROM video WHERE sort='${sort}' ORDER BY RAND() LIMIT 6`;
        try {
            let list = await Mysql(sql);
            let listStr = Buffer.from(JSON.stringify(list)).toString('base64').replace(/A/g, '&').replace(/B/g, '%'); //第一次转base64
            ctx.state.data = {
                list: listStr,
                // total: total[0].total,
                code: '001'
            }
        } catch (error) {
            ctx.state.data = {
                code: '002',
                err: 'مەغلۇپ بولدى'
            }
        }
    }
    // 获取小程序主页电影
    if (!from && ctx.query.pageSize && ctx.query.currentPage) {
        let {
            currentPage,
            pageSize
        } = ctx.query;
        let data = {};
        // 推荐
        try {
            let sql = `SELECT video.*,recommend.* FROM video RIGHT JOIN recommend on video.vid=recommend.vid`;
            let res = await Mysql(sql);
            data.recommend = res;
        } catch (error) {
            ctx.state.data = {
                code: '002',
                err: 'مەغلۇپ بولدى'
            }
        }
        // 火视频
        try {
            let sql = `SELECT video.* from (SELECT * from video ORDER BY views DESC)video LIMIT 12`;
            let res = await Mysql(sql);
            data.hotList = res;
        } catch (error) {
            ctx.state.data = {
                code: '002',
                err: 'مەغلۇپ بولدى'
            }
        }
        // 最新添加
        try {
            let sql = `SELECT video.* from (SELECT * from video ORDER BY create_time DESC)video  LIMIT 12`;
            let res = await Mysql(sql);
            data.newList = res;
        } catch (error) {
            ctx.state.data = {
                code: '002',
                err: 'مەغلۇپ بولدى'
            }
        }
        // 电视剧
        try {
            let sql = `SELECT video.* from (SELECT * from video WHERE sort='TV' ORDER BY create_time DESC)video  LIMIT 12`;
            let res = await Mysql(sql);
            data.tv = res;
        } catch (error) {
            ctx.state.data = {
                code: '002',
                err: 'مەغلۇپ بولدى'
            }
        }
        // 短视频
        try {
            let sql = `SELECT video.* from (SELECT * from video WHERE sort='SHORT' ORDER BY create_time DESC)video  LIMIT 12`;
            let res = await Mysql(sql);
            data.shortList = res;
        } catch (error) {
            ctx.state.data = {
                code: '002',
                err: 'مەغلۇپ بولدى'
            }
        }
        // 综艺-舞蹈
        try {
            let sql = `SELECT video.* from (SELECT * from video WHERE sort='VARIETY' ORDER BY create_time DESC)video  LIMIT 12`;
            let res = await Mysql(sql);
            data.varietyList = res;
        } catch (error) {
            ctx.state.data = {
                code: '002',
                err: 'مەغلۇپ بولدى'
            }
        }
        let listStr = Buffer.from(JSON.stringify(data)).toString('base64').replace(/A/g, '&').replace(/B/g, '%'); //第一次转base64
        // list = Buffer.from(listStr).toString('base64') // 第二次转
        ctx.state.data = {
            list: listStr,
            // total: total[0].total,
            code: '001'
        }
        /*   try {
              let sql = `SELECT * from video LIMIT ${(currentPage-1)*pageSize},${pageSize}`;
              // let totalSql = `SELECT COUNT(*) as total from video`
              let list = await Mysql(sql); */
        // let total = await Mysql(totalSql);
        /**
         * @key {所有的A换成&,B换成%}
         */

        /*  } catch (error) {
             ctx.state.data = {
                 code: '002',
                 err: 'مەغلۇپ بولدى'
             }
         } */
    }
};